package com.example.caimogu.http;

import android.util.Log;


import com.example.caimogu.Goods2Json.String2Pic;
import com.example.caimogu.bean.Goods;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.FileEntity;

import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Caimogu on 2016/4/20.
 */
public class HttpClientUtil {
    private String url = "http://115.159.220.253:8080/StuMs/";
    //private String url = "http://192.168.1.101:8080/StuMs/";


    public void sendNewGoods2Server(JSONArray jsonArrayGoodsList, List<String> fileStringListParent, List<Goods> gl){

        Log.i("caimogu",jsonArrayGoodsList.toString());
        Log.i("caimogu", "图片数："+fileStringListParent.size());
        String str = "putonGoods";

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost  = new HttpPost(url+str);
        try {
            List<NameValuePair> postParameters = new ArrayList<>();

            postParameters.add(new BasicNameValuePair("json", jsonArrayGoodsList.toString()));
            List<String> listGoodsPic = new ArrayList<String>();
            for(Goods g:gl){
                for(String s:g.getPicPath()){
                    listGoodsPic.add(s);

                }
            }
            Log.i("caimogu", "图片字符数："+fileStringListParent.size());
            for(int i=0; i<fileStringListParent.size(); i++){
                postParameters.add(new BasicNameValuePair(listGoodsPic.get(i),fileStringListParent.get(i)));
            }


            httpPost.setEntity(new UrlEncodedFormEntity(postParameters, HTTP.UTF_8));
            HttpResponse response = httpClient.execute(httpPost); //执行POST方法

        } catch (Exception e) {
            e.printStackTrace();
            Log.i("caimogu","HttpClientUtil...sendNewGoods2Server()...error");
        }


    }

    public HttpResponse getMyOrders(String id){
        Log.i("caimogu","getMyOrders...");
        //String url = "http://192.168.1.110:8080/StuMs/queryBusinessOrders?bid=" + id;
        String str = "queryBusinessOrders?bid="+id;
        try {

            HttpClient client = new DefaultHttpClient();

            HttpGet request = new HttpGet();
            request.setURI(new URI(url+str));
            HttpResponse response = client.execute(request);

            return response;
        }catch(Exception e){
            e.printStackTrace();
        }

        return  null;
    }

    public void sendBus2Server(JSONArray jsonArray){

        //String url = "http://192.168.1.105:8080/StuMs/businessRegist";
        String str = "businessRegist";
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost  = new HttpPost(url+str);
        try {
            List<NameValuePair> postParameters = new ArrayList<>();
            //JSONObject jo = myJSON.getOrderJSON(order);
            Log.i("caimogu", jsonArray.get(0).toString());

            postParameters.add(new BasicNameValuePair("json", jsonArray.toString()));

            httpPost.setEntity(new UrlEncodedFormEntity(postParameters, HTTP.UTF_8));
            HttpResponse response = httpClient.execute(httpPost); //执行POST方法
            Log.i("","HttpClientUtil...sendNewGoods2Server()...");

        } catch (Exception e) {
            e.printStackTrace();
            Log.i("", "HttpClientUtil...sendNewGoods2Server()...error");
        }
        //return null;

    }
    //登录，并返回唯一的id
    public HttpResponse businessLogIn(String account,String psd){
        String str = "businessLogin?account="+account+"&psd="+psd;
        try {

            HttpClient client = new DefaultHttpClient();

            HttpGet request = new HttpGet();
            request.setURI(new URI(url+str));
            HttpResponse response = client.execute(request);

            return response;
        }catch(Exception e){
            e.printStackTrace();
        }

        return null;
    }
    public void changeOrdersStatus(String oid, int status){
        String url = "changeOrderStatus?status="+status+"&id="+oid;
        try {

            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet();
            request.setURI(new URI(url));
            HttpResponse response = client.execute(request);

        }catch(Exception e){
            e.printStackTrace();
        }
    }

}
